<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>测试reactive</h2>
      <p id="name"></p>
      <p id="name2"></p>
      <p id="age"></p>
      <p id="sex"></p>
    </div>

    <script type="module">
      // effect: 用来注册副作用函数的函数
      // 定义reactive对象

      import { reactive, effect } from './04_响应式.js'

      /* 
      一个属性对应多个effect函数  用set保存多个effect函数
      一个对象中有多个属性, 对应有多个set, 用map来保存多个set, key是属性名, value是set
      多个对象, 对应多个map, 整个用一个大的map来保存多个map, key是对象, value是map
      \


      */
      const state = reactive({
        name: 'tom',
        age: 12
      })
      const state2 = reactive({
        name2: 'tom',
        age2: 12
      })

      effect(function effect1() {
        console.log('name effect')
        document.getElementById('name').innerHTML = state.name
      })

      effect(function effect1() {
        console.log('name2 effect')
        document.getElementById('name2').innerHTML = state.name
      })

      effect(function effect2() {
        console.log('age effect')
        document.getElementById('age').innerHTML = state.age
      })
      effect(function effect2() {
        console.log('sex effect')
        document.getElementById('sex').innerHTML = state.sex
      })

      setTimeout(() => {
        // 更新已有属性
        state.name = 'Jack'
        state.age = 13
        // 添加新属性
        // state.sex = '男'
        // delete state.name
      }, 2000);
    </script>
  </body>
</html>
